package com.hdoit.apollo.web.stock.providers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.PageContext;

import org.springframework.util.StringUtils;

import com.hdoit.apollo.logical.inStock.model.InStock;
import com.hdoit.apollo.logical.inStock.service.IInStockService;
import com.tennetcn.core.common.message.ListItem;
import com.tennetcn.core.common.utils.CommonApplicationContextUtil;
import com.tennetcn.core.common.utils.PageContextUtils;
import com.tennetcn.setting.logical.model.ParamOption;
import com.tennetcn.setting.logical.service.IParamOptionService;
import com.tennetcn.web.component.attribute.provider.IMultipleOptionListProvider;

public class WarehouseProvider implements  IMultipleOptionListProvider<ListItem>{

	IInStockService inStockService = CommonApplicationContextUtil.getCurrentContext().getBean(IInStockService.class);
	IParamOptionService paramOptionService = CommonApplicationContextUtil.getCurrentContext().getBean(IParamOptionService.class);
	private List<ListItem> dataList=null;
	
	@Override
	public List<ListItem> getDataList(Object viewData) {
		List<ParamOption> paramOptions = paramOptionService.queryListByParamName("warehouse");
		dataList = new ArrayList<ListItem>();
		if(paramOptions == null) {return dataList; }
		ListItem listItem=null;
		for(ParamOption paramOption : paramOptions) {
			listItem=new ListItem();
	    	listItem.setValue(paramOption.getParamValue());
	    	listItem.setText(paramOption.getParamText());
	    	dataList.add(listItem);
		}
		return dataList;
	}


	
	@Override
	public List<ListItem> getSelectedItems(Object value, Object viewData) {
		PageContext pageContext=(PageContext)viewData;
		String key= PageContextUtils.getParam(pageContext, "key");
		InStock inStock = inStockService.queryModel(key);
		if(!StringUtils.isEmpty(inStock)){
			if(!StringUtils.isEmpty(inStock.getWarehouse())){
				String warehouses = inStock.getWarehouse();
				if(warehouses !=null && dataList!=null){
					List<ListItem> selListItem=new ArrayList<ListItem>();
					for (ListItem listItem : dataList) {
						if(warehouses.contains(listItem.getValue())){
							selListItem.add(listItem);
						}
					}
					return selListItem;
				}
			}
		}
		return null;
	}

}
